package com.project.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.project.bean.GradeBean;
import com.project.bean.RoleBean;
import com.project.service.IRoleService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
@RequestMapping("role")
@Api(tags = "用户角色控制器")
public class RoleController {
    @Autowired
    private IRoleService service;

    @RequiresPermissions("grade:role")
    @RequestMapping("findByRoleIdArray")
    @ApiOperation(value = "查看角色", notes = "查看角色")
    @ApiImplicitParams(value = {
            @ApiImplicitParam(name = "roleIdArray", value = "角色id数组")
    })
    public String findByRoleIdArray(Integer[] roleIdArray) {
        return service.findByRoleIdArray(roleIdArray);
    }

    @RequiresPermissions("grade:role")
    @RequestMapping("cutByItem")
    @ApiOperation(value = "角色分页", notes = "角色分页")
    @ApiImplicitParams(value = {
            @ApiImplicitParam(name = "pageNO", value = "页码",required = true)
    })
    public IPage<RoleBean> cutByItem(Integer pageNO) {
        return service.cutByItem(pageNO);
    }

    @RequiresPermissions("grade:role")
    @RequestMapping("delRole")
    @ApiOperation(value = "删除某个角色", notes = "删除某个角色")
    @ApiImplicitParams(value = {
            @ApiImplicitParam(name = "roleId", value = "角色id")
    })
    public String delRole(Integer roleId) {
        service.delRole(roleId);
        return "ok";
    }

    @RequiresPermissions("grade:role")
    @RequestMapping("findAllRole")
    @ApiOperation(value = "查看所有角色", notes = "查看所有角色")
    public List<RoleBean> findAllRole() {
        return service.findAllRole();
    }

    @RequiresPermissions("grade:role")
    @RequestMapping("findByRoleId")
    @ApiOperation(value = "根据角色id查看角色", notes = "根据角色id查看角色")
    @ApiImplicitParams(value = {
            @ApiImplicitParam(name = "roleId", value = "角色id")
    })
    public RoleBean findByRoleId(Integer roleId) {
        return service.findByRoleId(roleId);
    }

    @RequiresPermissions("grade:role")
    @RequestMapping("updateGrade")
    @ApiOperation(value = "改变角色拥有的权限", notes = "改变角色拥有的权限")
    @ApiImplicitParams(value = {
            @ApiImplicitParam(name = "roleId", value = "角色id"),
            @ApiImplicitParam(name = "newGradeIdArray", value = "新的权限数组")
    })
    public String updateGrade(Integer roleId, Integer[] newGradeIdArray) {
        service.updateGrade(roleId, newGradeIdArray);
        return "ok";
    }

    @RequiresPermissions("grade:role")
    @RequestMapping("addRole")
    @ApiOperation(value = "添加角色", notes = "添加角色")
    @ApiImplicitParams(value = {
            @ApiImplicitParam(name = "roleName", value = "角色名称"),
            @ApiImplicitParam(name = "gradeIdArray", value = "该角色拥有的权限数组")
    })
    public String addRole(String roleName, Integer[] gradeIdArray) {
        service.addRole(new RoleBean(roleName), gradeIdArray);
        return "ok";
    }

    @RequiresPermissions("grade:role")
    @RequestMapping("findRoleListByUserId")
    @ApiOperation(value = "查看用户的角色", notes = "查看用户的角色")
    @ApiImplicitParams(value = {
            @ApiImplicitParam(name = "userId", value = "用户id")
    })
    public List<RoleBean> findRoleListByUserId(Integer userId) {
        return service.findRoleListByUserId(userId);
    }

    @RequiresPermissions("grade:role")
    @RequestMapping("delByUser")
    @ApiOperation(value = "删除用户拥有的角色", notes = "删除用户拥有的角色")
    @ApiImplicitParams(value = {
            @ApiImplicitParam(name = "userId", value = "用户id")
    })
    public String delByUser(Integer userId) {
        service.delByUser(userId);
        return "ok";
    }
}
